<template>
    <div class="login-container">
        <el-form :model="loginForm" :rules="rules" ref="loginForm" class="login-form">
            <h3 class="title">图书管理系统登录</h3>
            <el-form-item prop="userCode">
                <el-input v-model="loginForm.userCode" type="text" placeholder="用户名">
                    <template slot="prepend">
                        <i class="el-icon-user"></i>
                    </template>
                </el-input>
            </el-form-item>
            <el-form-item prop="password">
                <el-input v-model="loginForm.password" type="password" placeholder="密码">
                    <template slot="prepend">
                        <i class="el-icon-lock"></i>
                    </template>
                </el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" style="width:100%;" @click="handleLogin" :loading="loading">登录</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import md5 from 'md5'

export default {
    name: 'Login',
    data() {
        return {
            loginForm: {
                userCode: '',
                password: ''
            },
            loading: false,
            rules: {
                userCode: [
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                    { min: 5, max: 5, message: '用户名长度必须为5位', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { min: 6, max: 20, message: '密码长度不能小于6位', trigger: 'blur' }
                ]
            }
        }
    },
    methods: {
        async handleLogin() {
            try {
                await this.$refs.loginForm.validate()
                this.loading = true
                
                const response = await this.$axios({
                    method: 'post',
                    url: '/api/login',  // 保持不变，代理会处理路径转换
                    data: {
                        userCode: this.loginForm.userCode,
                        password: md5(this.loginForm.password)
                    }
                })
                
                if (response.data.code === 0) {
                    localStorage.setItem('token', response.data.data.token)
                    localStorage.setItem('userCode', response.data.data.userCode)
                    this.$message.success('登录成功')
                    await this.$router.push('/book/list')
                } else {
                    this.$message.error(response.data.msg || '登录失败')
                }
            } catch (error) {
                console.error('登录失败:', error)
                this.$message.error('登录失败，请重试')
            } finally {
                this.loading = false
            }
        }
    }
}
</script>

<style>
@import 'element-ui/lib/theme-chalk/index.css';

.login-container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    background-color: #f3f3f3;
}

.login-form {
    width: 350px;
    padding: 35px;
    border-radius: 5px;
    background: #fff;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, .1);
}

.title {
    text-align: center;
    margin: 0 0 30px;
    color: #303133;
}
</style>